package com.example.city_home_app.Model;

import android.net.Uri;

import com.example.city_home_app.ApiIface;
import com.example.city_home_app.Bean.Down_Captcha;
import com.example.city_home_app.Bean.Down_City;
import com.example.city_home_app.Bean.Down_Community;
import com.example.city_home_app.Bean.Down_Login;
import com.example.city_home_app.Bean.Down_Moment;
import com.example.city_home_app.Bean.Down_MomentComment;
import com.example.city_home_app.Bean.Down_Msg;
import com.example.city_home_app.Bean.Down_MsgCencter;
import com.example.city_home_app.Bean.Down_MyFocus;
import com.example.city_home_app.Bean.Down_News;
import com.example.city_home_app.Bean.Down_NewsComment;
import com.example.city_home_app.Bean.Down_NewsReply;
import com.example.city_home_app.Bean.Down_Post;
import com.example.city_home_app.Bean.Down_PostComment;
import com.example.city_home_app.Bean.Down_PostReply;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Bean.Down_Update;
import com.example.city_home_app.Bean.Down_UserInfo;
import com.example.city_home_app.Bean.Down__Image;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.Service;
import com.example.city_home_app.UI.UIUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ApiModel extends BaseModel implements ApiIface {

    private Service service;
    private Retrofit retrofit;

    public ApiModel() {
        retrofit = getRetrofit();
        service = retrofit.create(Service.class);
    }

    public ApiModel(int type) {
        retrofit = getRetrofit(1);
        service = retrofit.create(Service.class);
    }

    @Override
    public void login(String username, String password, RetrofitListener listener) {
        Call<Down_Login> call = service.login(username,password);
        callenqueue(call,listener);
    }

    @Override
    public void reg(String loginname, String email, String code, int codeid, int cityid, String username, String password, int gender, RetrofitListener listener) {
        Call<Down_Universe> call = service.reg(loginname,email,code,codeid,cityid,gender,username,password);
        callenqueue(call,listener);
    }

    @Override
    public void forgotpassword(String email, String code, int codeid, String password, RetrofitListener listener) {
        Call<Down_Universe> call = service.forgotuserpassword(email,code,codeid,password);
        callenqueue(call,listener);
    }

    @Override
    public void getcheckcode(String email, RetrofitListener listener) {
        Call<Down_Captcha> call = service.getcheckcode(email);
        callenqueue(call,listener);
    }

    @Override
    public void getcitylist(RetrofitListener listener) {
        Call<List<Down_City>> call = service.getcitylist();
        callenqueue(call,listener);
    }

    @Override
    public void getnewlist(int cityid, RetrofitListener listener) {
        Call<List<Down_News>> call = service.getnewlist(cityid);
        callenqueue(call,listener);
    }

    @Override
    public void getnews(int newsid, RetrofitListener listener) {
        Call<Down_News> call = service.getnew(newsid);
        callenqueue(call,listener);
    }

    @Override
    public void addnewsHot(int newsid, RetrofitListener listener) {
        Call<Down_Universe> call = service.addnewsHot(newsid);
        callenqueue(call,listener);
    }

    @Override
    public void getNewsComment(int newsid, RetrofitListener listener) {
        Call<List<Down_NewsComment>> call = service.getNewsComment(newsid);
        callenqueue(call,listener);
    }

    @Override
    public void getReplyNewComment(int commentid, RetrofitListener listener) {
        Call<List<Down_NewsReply>> call = service.getReplyNewComment(commentid);
        callenqueue(call,listener);
    }

    @Override
    public void likenewcomment(int userid, int commentid, int newsid, RetrofitListener listener) {
        Call<Down_Universe> call = service.likenewcomment(userid,commentid,newsid);
        callenqueue(call,listener);
    }


    @Override
    public void unlikenewcomment(int userid, int commentid, int newsid, RetrofitListener listener) {
        Call<Down_Universe> call = service.unlikenewcomment(userid,commentid,newsid);
        callenqueue(call,listener);
    }

    @Override
    public void islikenewcomment(int userid, int commentid, RetrofitListener listener) {
        Call<Down_Universe> call = service.islikenewcomment(userid,commentid);
        callenqueue(call,listener);
    }

    @Override
    public void getlikenewcomment(int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.getlikenewcomment(userid);
        callenqueue(call,listener);
    }

    @Override
    public void upnewcomment(int newsid, int userid, String content, RetrofitListener listener) {
        Call<Down_Universe> call = service.upnewcomment(newsid,userid,content);
        callenqueue(call,listener);
    }

    @Override
    public void replynewcomment(int newsid, int commentid, int userid, String content, RetrofitListener listener) {
        Call<Down_Universe> call = service.replynewcomment(newsid,commentid,userid,content);
        callenqueue(call,listener);
    }

    @Override
    public void getcommunitylist(RetrofitListener listener) {
        Call<List<Down_Community>> call = service.getcommunitylist();
        callenqueue(call,listener);
    }


    @Override
    public void focuscommunity(int communityid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.focuscommunity(communityid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void unfocuscommunity(int communityid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.unfocuscommunity(communityid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void isfocuscommunity(int communityid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.isfocuscommunity(communityid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void getpostlist(int type, int cityid, RetrofitListener listener) {
        Call<List<Down_Post>> call = service.getpostlist(type,cityid);
        callenqueue(call,listener);
    }

    @Override
    public void getpostlistforcommunity(int type, int cityid, int communityid, RetrofitListener listener) {
        Call<List<Down_Post>> call = service.getpostlistforcommunity(type,cityid,communityid);
        callenqueue(call,listener);
    }

    @Override
    public void searchpost(String search, int cityid, RetrofitListener listener) {
        Call<List<Down_Post>> call = service.searchpost(search,cityid);
        callenqueue(call,listener);
    }



    @Override
    public void uppostimgs(ArrayList paths, final RetrofitListener listener) {
        ArrayList<String> list = paths;
        Map<String,RequestBody> map =new HashMap<>();
        for(int i =0 ;i<list.size() ;i++){
            File file = new File(list.get(i));
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part body =MultipartBody.Part.createFormData("image", file.getName(), requestFile);
            map.put("image["+i+"]\";filename=\""+file.getName(),body.body());
        }
        Call<List<Down__Image>> call = service.uppostimgs(map);
        call.enqueue(new Callback<List<Down__Image>>() {
            @Override
            public void onResponse(Call<List<Down__Image>> call, Response<List<Down__Image>> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<Down__Image>> call, Throwable t) {

            }
        });
    }

    @Override
    public void delpostimgs(String urls, RetrofitListener listener) {
        Call<Down_Universe> call = service.delpostimgs(urls);
        callenqueue(call,listener);
    }

    @Override
    public void uppost(int cityid, int communityid, String title, String content, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.uppost(cityid,communityid,title,content,userid);
        callenqueue(call,listener);
    }

    @Override
    public void updatepost(int postid, String title, String content, RetrofitListener listener) {
        Call<Down_Universe> call = service.updatepost(postid,title,content);
        callenqueue(call,listener);
    }

    @Override
    public void getpost(int postid, RetrofitListener listener) {
        Call<Down_Post> call = service.getpost(postid);
        callenqueue(call,listener);
    }

    @Override
    public void focuspost(int postid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.focuspost(postid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void unfocuspost(int postid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.unfocuspost(postid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void isfocuspost(int postid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.isfocuspost(postid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void likepost(int postid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.likepost(postid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void unlikespost(int postid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.unlikepost(postid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void islikepost(int postid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.islikepost(postid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void uppostcomment(int postid, int userid, String content, RetrofitListener listener) {
        Call<Down_Universe> call = service.uppostcomment(postid,userid,content);
        callenqueue(call,listener);
    }

    @Override
    public void delpostcomment(int commentid, RetrofitListener listener) {
        Call<Down_Universe> call = service.delpostcomment(commentid);
        callenqueue(call,listener);
    }

    @Override
    public void replypostcomment(int commentid, int userid, String content,int postid, RetrofitListener listener) {
        Call<Down_Universe> call = service.replypostcomment(commentid,userid,content,postid);
        callenqueue(call,listener);
    }

    @Override
    public void delreplypostcomment(int replyid, RetrofitListener listener) {
        Call<Down_Universe> call = service.delreplypostcomment(replyid);
        callenqueue(call,listener);
    }

    @Override
    public void getpostcomment(int postid, RetrofitListener listener) {
        Call<List<Down_PostComment>> call = service.getpostcomment(postid);
        callenqueue(call,listener);
    }

    @Override
    public void getpostcommentreply(int commentid, RetrofitListener listener) {
        Call<List<Down_PostReply>> call = service.getpostcommentreply(commentid);
        callenqueue(call,listener);
    }

    @Override
    public void getmomentlist(int cityid, RetrofitListener listener) {
        Call<List<Down_Moment>> call = service.getmomentlist(cityid);
        callenqueue(call,listener);
    }

    @Override
    public void getmomentlistforfocus(int userid, int cityid, RetrofitListener listener) {
        Call<List<Down_Moment>> call = service.getmomentlistforfocus(userid,cityid);
        callenqueue(call,listener);
    }

    @Override
    public void likemoment(int momentid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.likemoment(momentid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void unlikemoment(int momentid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.unlikemoment(momentid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void islikemoment(int momentid, int userid, RetrofitListener listener) {
        Call<Down_Universe> call = service.islikemoment(momentid,userid);
        callenqueue(call,listener);
    }

    @Override
    public void getmoment(int momentid, RetrofitListener listener) {
        Call<Down_Moment> call = service.getmoment(momentid);
        callenqueue(call,listener);
    }

    @Override
    public void getmomentcomment(int momentid, RetrofitListener listener) {
        Call<List<Down_MomentComment>> call = service.getmomentcomment(momentid);
        callenqueue(call,listener);
    }

    @Override
    public void upmomentcomment(int isreply, int userid, int momentid, String content, String pic, RetrofitListener listener) {
        Call<Down_Universe> call = service.upmomentcomment(isreply,userid,momentid,content,pic);
        callenqueue(call,listener);
    }

    @Override
    public void upmoment(int type, int userid, String content, ArrayList file, final RetrofitListener listener) {
        //ArrayList<Uri> list = file;
        Map<String,RequestBody> map = new HashMap<>();
        if(type == 1){
            Call<Down_Universe> call = service.upmoment(type,userid,content);
            callenqueue(call,listener);
            return;
        }else if(type==2){
            ArrayList<String> list = file;
            for(int i =0;i<list.size();i++){
                File imgfile = new File(list.get(i));
                RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), imgfile);
                MultipartBody.Part body =MultipartBody.Part.createFormData("image", imgfile.getName(), requestFile);
                map.put("image["+i+"]\";filename=\""+imgfile.getName(),body.body());
            }
        }else if(type==3){
            ArrayList<String> list = file;
            File videofile = new File(list.get(0));
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), videofile);
            MultipartBody.Part body =MultipartBody.Part.createFormData("image", videofile.getName(), requestFile);
            map.put("video\";filename=\""+videofile.getName(),body.body());
        }


        Call<Down_Universe> call = service.upmomentfile(type,userid,content,map);
        call.enqueue(new Callback<Down_Universe>() {
            @Override
            public void onResponse(Call<Down_Universe> call, Response<Down_Universe> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<Down_Universe> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }

    @Override
    public void getuserinfo(int userid, RetrofitListener listener) {
        Call<Down_UserInfo> call = service.getuser(userid);
        callenqueue(call,listener);
    }

    @Override
    public void focususer(int userid, int userfid, RetrofitListener listener) {
        Call<Down_Universe> call = service.focususer(userid,userfid);
        callenqueue(call,listener);
    }

    @Override
    public void unfocususer(int userid, int userfid, RetrofitListener listener) {
        Call<Down_Universe> call = service.unfocususer(userid,userfid);
        callenqueue(call,listener);
    }

    @Override
    public void isfocususer(int userid, int userfid, RetrofitListener listener) {
        Call<Down_Universe> call = service.isfocususer(userid,userfid);
        callenqueue(call,listener);
    }

    @Override
    public void getusermomentlist(int userid, RetrofitListener listener) {
        Call<List<Down_Moment>> call = service.getusermomentlist(userid);
        callenqueue(call,listener);
    }

    @Override
    public void getuserpostlist(int userid, RetrofitListener listener) {
        Call<List<Down_Post>> call = service.getuserpostlist(userid);
        callenqueue(call,listener);
    }


    @Override
    public void getmyfocuslist(int userid, RetrofitListener listener) {
        Call<Down_MyFocus> call = service.getmyfocuslist(userid);
        callenqueue(call,listener);
    }

    @Override
    public void getmyfocuspostlist(int userid, RetrofitListener listener) {
        Call<List<Down_Post>> call = service.getmyfocuspostlist(userid);
        callenqueue(call,listener);
    }

    @Override
    public void getmypostlikelist(int userid, RetrofitListener listener) {
        Call<List<Down_Post>> call = service.getmypostlikelist(userid);
        callenqueue(call,listener);
    }

    @Override
    public void getmymomentlikelist(int userid, RetrofitListener listener) {
        Call<List<Down_Moment>> call = service.getmymomentlikelist(userid);
        callenqueue(call,listener);
    }

    @Override
    public void updateusername(int userid, String name, RetrofitListener listener) {
        Call<Down_Universe> call = service.updateusername(userid,name);
        callenqueue(call,listener);
    }

    @Override
    public void updateuserintro(int userid, String intro, RetrofitListener listener) {
        Call<Down_Universe> call = service.updateuserintro(userid,intro);
        callenqueue(call,listener);
    }

    @Override
    public void updateusergender(int userid, int gender, RetrofitListener listener) {
        Call<Down_Universe> call = service.updateusergender(userid,gender);
        callenqueue(call,listener);
    }

    @Override
    public void updateuserhaedimage(int userid, String path,RetrofitListener listener) {
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("image", file.getName(), requestFile);
        Map<String, RequestBody> map = new HashMap<>();
        map.put("userid", toRequestBody(userid + ""));
        Call<Down_Universe> call = service.updateuserhaedimage(body, map);
        callenqueue(call, listener);
    }

    @Override
    public void getmessagelist(int userid, RetrofitListener listener) {
        Call<List<Down_MsgCencter>> call = service.getmessagelist(userid);
        callenqueue(call,listener);
    }

    @Override
    public void getmessage(int userid1, int userid2, RetrofitListener listener) {
        Call<List<Down_Msg>> call = service.getmessage(userid1,userid2);
        callenqueue(call,listener);
    }

    @Override
    public void sendnormalmessage(int userid1, int userid2, int type, String content,RetrofitListener listener) {
        Call<Down_Universe> call = service.sendnormalmessage(userid1,userid2,content,type);
        callenqueue(call,listener);
    }

    @Override
    public void sendimagemessage(int userid1, int userid2, int type, String path,RetrofitListener listener) {
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("pic", file.getName(), requestFile);
        Map<String, RequestBody> map = new HashMap<>();
        map.put("userid1", toRequestBody(userid1 + ""));
        map.put("userid2", toRequestBody(userid2 + ""));
        map.put("type", toRequestBody(type+""));
        Call<Down_Universe> call = service.sendimagemessage(body, map);
        callenqueue(call, listener);
    }

    @Override
    public void getversion(RetrofitListener listener) {
        Call<Down_Update> call = service.getversion();
        callenqueue(call,listener);
    }

    @Override
    public void delmoment(int momentid, RetrofitListener listener) {
        Call<Down_Universe> call = service.delmoment(momentid);
        callenqueue(call,listener);
    }

    @Override
    public void delpost(int postid, RetrofitListener listener) {
        Call<Down_Universe> call = service.delpost(postid);
        callenqueue(call,listener);
    }
}
